package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.pojo.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.OrderService;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.web.bind.annotation.*;

/**
 * C端：订单控制器层
 * @author zgp
 * @version V1.0
 * @date 2025-01-28 11:31
 */
@Slf4j
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpMessageConverters messageConverters;

    @ApiOperation("获取订单预计送达时间")
    @GetMapping("/getEstimatedDeliveryTime")
    public Result<String> getOrderEstimatedDeliveryTime(Long shopId, String customerAddress){
        log.info("获取订单预计送达时间，shopId = {}，customerAddress = {}", shopId, customerAddress);
        return Result.success(orderService.getOrderEstimatedDeliveryTime(shopId, customerAddress));
    }

    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrders(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单，ordersSubmitDTO = {}", ordersSubmitDTO);
        return Result.success(orderService.submitOrders(ordersSubmitDTO));
    }

    @ApiOperation("用户催单")
    @GetMapping("/reminder/{id}")
    public Result<String> reminder(@PathVariable Long id){
        log.info("用户：{}催单, 订单id = {}", BaseContext.getCurrentId(), id);
        orderService.reminder(id);
        return Result.success();
    }

    @ApiOperation("查询历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult<Orders>> getHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("用户：{}，查询历史订单，ordersPageQueryDTO = {}", BaseContext.getCurrentId(), ordersPageQueryDTO);
        return Result.success(orderService.getUserHistoryOrders(ordersPageQueryDTO));
    }

    @ApiOperation("查询历史退款订单")
    @GetMapping("/queryOrdersCheckStatus")
    public Result<PageResult<Orders>> getRefundOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("用户：{}，查询历史退款订单，ordersPageQueryDTO = {}", BaseContext.getCurrentId(), ordersPageQueryDTO);
        return Result.success(orderService.getUserRefundOrders(ordersPageQueryDTO));
    }

    @ApiOperation("订单详情查看")
    @GetMapping("/orderDetail/{id}")
    public Result<Orders> getOrdersDetail(@PathVariable Long id){
        log.info("订单详情查看：id = {}", id);
        return Result.success(orderService.getOrdersDetail(id));
    }

    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result<String> repetitionOrders(@PathVariable Long id){
        log.info("再来一单：id = {}", id);
        orderService.repetitionBuyOrders(id);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result<String> cancelOrders(@PathVariable OrdersCancelDTO ordersCancelDTO){
        log.info("用户取消订单：ordersCancelDTO = {}", ordersCancelDTO);
        orderService.cancelOrders(ordersCancelDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }


}
